<template>
	<view>
		<u-form :model="form" ref="uForm" :rules="rules" class="from">
			<u-form-item labelWidth="120" label="手机号" prop="phonenumber">
				<u-input placeholder="请输入手机号" v-model="form.phonenumber" />
			</u-form-item>
			<u-form-item labelWidth="80" label="密码" prop="password">
				<u-input placeholder="请输入密码" v-model="form.password" />
			</u-form-item>
		</u-form>
		<u-toast ref="uToast"></u-toast>
		<u-button @click="submit" type="primary">登录</u-button>
		<view style="height:30px;"></view>


		<u-button type="primary" @click="toRegisterPage">
			去注册
		</u-button>
	</view>
</template>

<script>
	import {
		login
	} from '@/api/login.js'
	export default {
		data() {
			return {
				form: {
					phonenumber: '',
					password: ''
				},
				rules: {
					phonenumber: [{
						required: true,
						message: '请输入手机号',
						trigger: ['blur', 'change']
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: ['blur', 'change']
					}],
				}
			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			submit() {
				this.$refs.uForm.validate().then(valid => {
					if(!valid){
						return
					}
					login(this.form).then(res => {
						console.log('login', res)
						uni.setStorageSync('token', res[0].user_id)
						uni.setStorageSync('phonenumber', res[0].phonenumber)
						let token = uni.getStorageSync('token')
						console.log("token", token)
						uni.switchTab({
							url:'/pages/index/index',
							fail(fail) {
								console.log('fail',fail)
							}
						})
					})
				}).catch(errors => {})
			},
			toRegisterPage() {
				uni.navigateTo({
					url: '/pages/register/register'
				})
			}
		},
	};
</script>
<style>
	.from {
		margin: 10px 15px;
	}
</style>
